<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="工号" prop="sillPerformanceNum">
        <el-select v-model="queryParams.sillPerformanceNum"  clearable @input="searchNewData" placeholder="员工工号">
          <el-option v-for="item in trees" :key="item.employee_num" :label="item.employee_num" :value="item.employee_num"/>
        </el-select>
      </el-form-item>


      <el-form-item label="姓名" prop="sillPerformanceName">
        <el-select v-model="queryParams.sillPerformanceName" clearable  placeholder="员工姓名">
          <el-option v-for="item in options" :key="item.employee_Name" :label="item.employee_Name" :value="item.employee_Name"/>
        </el-select>
      </el-form-item>

      <el-form-item label="签订时间">
        <el-date-picker
          v-model="queryParams.sillPerformanceTime"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>

      <el-form-item label="合同单号" prop="sillPerformanceContract">
        <el-select v-model="queryParams.sillPerformanceContract" @input="getContractName" clearable  placeholder="合同单号">
          <el-option v-for="item in danHao" :key="item.sill_performance_contract" :label="item.sill_performance_contract" :value="item.sill_performance_contract"/>
        </el-select>
      </el-form-item>
      <el-form-item label="项目名称" prop="sillPerformanceProject">
        <el-select v-model="queryParams.sillPerformanceProject" clearable  placeholder="员工姓名">
          <el-option v-for="item in contractName" :key="item.sill_performance_project" :label="item.sill_performance_project" :value="item.sill_performance_project"/>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['performance:performance:add']"
        >新增</el-button>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="performanceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" type="index" />
      <el-table-column label="姓名" align="center" prop="sillPerformanceName" />
      <el-table-column label="工号" align="center" prop="sillPerformanceNum" />
      <el-table-column label="签订年月" align="center" prop="sillPerformanceDate" />
      <el-table-column label="签订时间" align="center" prop="sillPerformanceTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.sillPerformanceTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="合同单号" align="center" prop="sillPerformanceContract" />
      <el-table-column label="项目名称" align="center" prop="sillPerformanceProject" />
      <el-table-column label="签订金额" align="center" prop="sillPerformanceMoney" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['performance:performance:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['performance:performance:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改员工绩效表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

        <el-form-item label="员工工号">
          <el-select v-model="form.sillPerformanceNum"  @input="searchData" placeholder="员工工号">
            <el-option v-for="item in trees" :key="item.employee_num" :label="item.employee_num" :value="item.employee_num"/>
          </el-select>
        </el-form-item>

        <el-form-item label="员工姓名">
          <el-select v-model="form.sillPerformanceName" placeholder="员工姓名">
            <el-option v-for="item in options" :key="item.employee_Name" :label="item.employee_Name" :value="item.employee_Name"/>
          </el-select>
        </el-form-item>

        <el-form-item label="签订时间" prop="sillPerformanceTime">
          <el-date-picker clearable
            v-model="form.sillPerformanceTime"
            type="date"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择签订时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="项目名称" prop="sillPerformanceProject">
          <el-input v-model="form.sillPerformanceProject" placeholder="请输入项目名称" />
        </el-form-item>
        <el-form-item label="签订金额" prop="sillPerformanceMoney">
          <el-input v-model="form.sillPerformanceMoney" placeholder="请输入签订金额" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPerformance, getPerformance, delPerformance, addPerformance, updatePerformance } from "@/api/performance/performance";
import {getEmployeeTree} from "@/api/rest/rest";
import request from "@/utils/request";

export default {
  name: "Performance",
  data() {
    return {
      contractName:[],
      danHao:[],
      trees:[],
      options:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 员工绩效表表格数据
      performanceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        date:"",
        pageNum: 1,
        pageSize: 10,
        sillPerformanceName: null,
        sillPerformanceNum: null,
        sillPerformanceDate: null,
        sillPerformanceTime: null,
        sillPerformanceContract: null,
        sillPerformanceProject: null,
        sillPerformanceMoney: null,
        startTime:null,
        endTime:null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询员工绩效表列表 */
    getList() {
      this.loading = true;
      let params = this.queryParams;
      if(params.sillPerformanceTime){
        params.startTime = params.sillPerformanceTime[0]?params.sillPerformanceTime[0]:null
        params.endTime = params.sillPerformanceTime[1]?params.sillPerformanceTime[1]:null
      }

      listPerformance(params).then(response => {
        this.performanceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        sillPerformanceName: null,
        sillPerformanceNum: null,
        sillPerformanceDate: null,
        sillPerformanceTime: null,
        sillPerformanceContract: null,
        sillPerformanceProject: null,
        sillPerformanceMoney: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.startTime = null;
      this.queryParams.endTime = null;
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.searchData();
      this.reset();
      this.open = true;
      this.title = "添加员工绩效表";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPerformance(id).then(response => {
        this.searchData();
        this.form = response.data;
        this.open = true;
        this.title = "修改员工绩效表";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePerformance(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPerformance(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除员工绩效表的数据项？').then(function() {
        return delPerformance(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    //得到工号下拉列表
    getArticleTypeTree() {
      getEmployeeTree().then(response => {
        this.trees = response.data;
      });
      this.getDanHao();
    },
    searchData(){
      request({
        url: '/rest/rest/treeName/' + this.form.sillPerformanceNum,
        method: 'get',
      }).then(resp=>{
        this.options = resp.data;

      })
    },
    searchNewData(){
      request({
        url: '/rest/rest/treeName/' + this.queryParams.sillPerformanceNum,
        method: 'get',
      }).then(resp=>{
        this.options = resp.data;

      })
    },
    getDanHao(){
      request({
        url: '/performance/performance/danHao',
        method: 'get',
      }).then(resp=>{
        this.danHao = resp.data;

      })
    },
    getContractName(){
      request({
        url: '/performance/performance/contractName/' + this.queryParams.sillPerformanceContract,
        method: 'get',
      }).then(resp=>{
        this.contractName = resp.data;

      })
    }
  },

  mounted() {
    this.getArticleTypeTree();

  }
};
</script>
